//
//  WBTabBar.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/9.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBTabBar.h"
#import "UIView+WBFrame.h"


@interface  WBTabBar()

@property (nonatomic , strong) UIButton *btn;

@end


@implementation WBTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

//    //添加控件 设置frame
    UIButton *btn = [[UIButton alloc] init];
    self.btn = btn;
    //添加按钮点击事件
    [btn addTarget:self action:@selector(composedButtonClick) forControlEvents:UIControlEventTouchUpInside];
    //设置普通状态图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    //设置选中的图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateSelected];
    //设置普通状态下的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    //设置选中状态下的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateSelected];
    
    //根据内容自动适应大小
    [self.btn sizeToFit];
    
    //添加控件
    [self addSubview:btn];
    
    return self;
    
}
- (void)composedButtonClick
{
    //调用代理，监听
    if([self.WBDelegate respondsToSelector:@selector(btnClick)])
    {
    //调用方法
        [self.WBDelegate btnClick];
    
    }


}

- (void)layoutSubviews
{
    
    [super layoutSubviews];

    //设置宽度
    CGFloat btnW = kScreenW * 0.2;
    
    NSInteger i = 0;
    
    //遍历子控件，设置frame
    for (UIButton *btn in self.subviews) {
        if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")])
        {
            
            btn.X = btnW * i;
            
            btn.W = btnW;

            i++;
         
            if(i == 2){
            
                i++;
            
            }
            
        }
    }
    
    //设置撰写按钮的frame
    CGPoint temp = self.btn.center;
    temp.x = self.bounds.size.width * 0.5;
    temp.y = self.bounds.size.height * 0.5;
   
    self.btn.center = temp;

}

@end
